//
//  ShopProductAddCheckImageViewController.m
//  newShop
//
//  Created by scjh on 15/5/20.
//  Copyright (c) 2015年 wangliang0. All rights reserved.
//

#import "ShopProductAddCheckImageViewController.h"
#import "ShopProductAddCheckImageView.h"
#import "WLAlertView.h"
#import "WLPhotoModel.h"

@interface ShopProductAddCheckImageViewController ()<ProtocolDelegate, UIImagePickerControllerDelegate,
  UINavigationControllerDelegate, UIAlertViewDelegate,UIActionSheetDelegate>

@property (nonatomic, strong) ShopProductAddCheckImageView *checkView;
@property (nonatomic, strong) UIImage *originImage;
@property (nonatomic, strong) UIActionSheet *actionSheet;

@end

@implementation ShopProductAddCheckImageViewController

- (instancetype)initWithImage:(UIImage *)image {
    
    if (self = [super init]) {
        
        self.originImage = image;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.checkView];
    self.checkView.photoImageView.image = self.originImage;
    
    self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"添加相片"
                                                   delegate:self
                                          cancelButtonTitle:@"取消"
                                     destructiveButtonTitle:nil
                                          otherButtonTitles:@"相册",@"相机", nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (ShopProductAddCheckImageView *)checkView {
    
    if (!_checkView) {
        
        CGRect frame = [UIApplication sharedApplication].keyWindow.bounds;
        _checkView = [[ShopProductAddCheckImageView alloc] initWithFrame:frame];
        _checkView.delegate = self;
    }
    return _checkView;
}

- (void)protocolDelegte:(id)object withInfo:(id)info {
    
    if ([object isKindOfClass:[ShopProductAddCheckImageView class]]) {
        
        NSInteger index = [info[@"index"] integerValue];
        switch (index) {
            case 1:
            {
                [self dismissViewControllerAnimated:YES completion:nil];
            }
                break;
            case 2:
            {
                UIAlertView *alertView =
                [[UIAlertView alloc] initWithTitle:@""
                                           message:@"是否删除图片"
                                          delegate:self
                                 cancelButtonTitle:@"取消"
                                 otherButtonTitles:@"确定", nil];
                [alertView show];
            }
                break;
            case 3:
            {
                [self.actionSheet showInView:self.view];
            }
                break;
                
            default:
                break;
        }
    }}


#pragma mark UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    UIImage *editImage = info[UIImagePickerControllerEditedImage];
    self.checkView.photoImageView.image = editImage;
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:K_Image_Change
                                                        object:self
                                                        userInfo:@{@"image":editImage}];
}

#pragma mark alertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 1) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:K_Image_Change
                                                            object:self
                                                          userInfo:@{@"deleteImage":self.checkView.photoImageView.image}];
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    NSDictionary *infoDic = nil;
    if (buttonIndex == 0) {
        
        infoDic = @{@"index":@(2)};
    }else if (buttonIndex == 1) {
        
        infoDic = @{@"index":@(3)};
    }else if (buttonIndex == 2) {
        infoDic = @{@"index":@(1)};
    }
    [WLPhotoModel photoModelActionObjectVC:self withDelegate:self withInfo:infoDic];
}

@end
